package com.wealth.lottery.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wealth.lottery.entity.LotteryActivity;
import com.wealth.lottery.entity.LotteryNumber;
import com.wealth.lottery.enums.LotteryActivityStatusEnums;
import com.wealth.lottery.enums.LotteryPurposeEnums;
import com.wealth.lottery.repository.LotteryActivityDao;
import com.wealth.lottery.repository.LotteryNumberDao;
import com.wealth.lottery.repository.LotteryRecordDao;
import com.wealth.lottery.service.ILotteryActivityService;
import com.wealth.member.entity.Member;
import com.wealth.order.entity.OrderPlat;
import com.wealth.order.repository.OrderPlatDao;

import iaas.enums.StatusEnum;
import iaas.enums.YesOrNoEnum;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.CommonUtil;
import iaas.utils.ResultObject;

/**
 * LotteryActivityServiceImpl
 * 
 * @author lihang 2016-06-27
 *
 */
@Component
public class LotteryActivityServiceImpl extends BaseServiceImpl<LotteryActivity> implements ILotteryActivityService {
	// 日志记录对象
	Logger logger = Logger.getLogger(LotteryActivityServiceImpl.class);
	@Autowired
	private LotteryActivityDao activityDao;
	@Autowired
	private LotteryNumberDao numberDao;
	@Autowired
	private OrderPlatDao orderPlatDao;
	@Autowired
	private LotteryRecordDao lotteryRecordDao;
	
	/**
	 * 查询该企业下，正在启用的抽奖活动
	 * @param corpid
	 * @return LotteryActivity对象
	 */
	@SuppressWarnings("finally")
	@Override
	public LotteryActivity getByCorpId(Long corpid,int type) throws Exception {
		LotteryActivity activity = null;
		try {
			List<LotteryActivity> activityList = activityDao.findByStatusAndCorpIdAndType(
					LotteryActivityStatusEnums.NORMAL.getValue(), corpid, type);
			if (activityList.size() > 0) {
				activity = activityList.get(0);
			} else {
				logger.info("没有查询到数据");
			}
		} catch (Exception e) {
			logger.error(e);
		} finally {
			return activity;
		}
	}
	@Override
	public List<LotteryActivity> getByStatus() throws Exception{
		return activityDao.findByStatus(StatusEnum.NORMAL.getValue());
	}
	@Override
	public List<LotteryActivity> getByStatusNot() throws Exception{
		return activityDao.findByStatusNot(StatusEnum.DELETE.getValue());
	}
	@Override
	public List<LotteryActivity> findByCorpIdAndStatus(Long corpid,
			Integer status) throws Exception {
		
		return activityDao.findByCorpIdAndStatus(corpid, status);
	}
	@Override
	public List<LotteryActivity> findByCorpIdAndStatusAndType(Long corpid,
			Integer status, Integer type) throws Exception {
		
		return activityDao.findByStatusAndCorpIdAndType(status, corpid, type);
	}
	@Override
	public LotteryActivity getByStatusAndCurrency(Long corpid) throws Exception {
		LotteryActivity activity = null;
		List<LotteryActivity> actiList=activityDao.findByCorpIdAndStatusAndScoretype(corpid, LotteryActivityStatusEnums.NORMAL.getValue(), LotteryPurposeEnums.CURRENCY.getValue());
		if(actiList.size()>0){
			activity=actiList.get(0);
		}
		return activity;
	}
	@Override
	public List<LotteryActivity> getByStatusAndCurrencyAll(Long corpid) throws Exception {
		List<LotteryActivity> actiList=activityDao.findByCorpIdAndStatusNotAndScoretype(corpid, LotteryActivityStatusEnums.DELETE.getValue(), LotteryPurposeEnums.CURRENCY.getValue());
		return actiList;
	}
	@Override
	public ResultObject getByScoreGt(Double money,Long corpid,Long memberid) throws Exception {
//		List<LotteryActivity> lotteryList= activityDao.findByCorpIdAndStatusAndScoreLessThanEqual(corpid, LotteryActivityStatusEnums.NORMAL.getValue(), money);
//		if(lotteryList.size()>0){
//			/**增加用户的抽奖次数*/
//			List<LotteryNumber> numberList= numberDao.findByMemberIdAndActivityId(memberid, lotteryList.get(0).getId());
//			if(numberList.size()>0){
//				LotteryNumber number = numberList.get(0);
//				number.setTotal(number.getTotal()+1);
//				numberDao.save(number);
//			}else{
//				LotteryNumber number = new LotteryNumber();
//				number.setTotal(1);
//				number.setActivity(lotteryList.get(0));
//				number.setMember(new Member(memberid));
//				numberDao.save(number);
//			}
//			return new ResultObject("000", "可以抽奖", lotteryList.get(0).getId());
//		}else{
//			return new ResultObject("500", "不能抽奖", null);
//		}
		
		//当前启用的抽奖活动
		List<LotteryActivity> lotteryList= activityDao.findByCorpIdAndStatus(1L, LotteryActivityStatusEnums.NORMAL.getValue());
		if(lotteryList.size()>0){
			//step1
			//每消费多少金额增加一次抽奖机会
			LotteryActivity activity = lotteryList.get(0);
			Double frequency = activity.getScore();
			//总共消费多少金额 paystatus，memberid ，corpid，日期大于等于10.2
			Date createTime = CommonUtil.dateStrToDate("yyyy-MM-dd", "2016-10-02");
			/*List<OrderPlat> orderPlats = orderPlatDao.findByCorpIdAndMemberIdAndPaystatusAndCreateTimeGreaterThanEqual(corpid, memberid,YesOrNoEnum.YES.getValue(), createTime);
			Double orderPlatTotal = 0d;
			for (OrderPlat orderPlat : orderPlats) {
				orderPlatTotal+=orderPlat.getActualPrice();
			}*/
			/**取最新支付的订单，得到应该赠送的抽奖次数*/
			List<OrderPlat> orderPlats = orderPlatDao.findByCorpIdAndMemberIdAndPaystatusAndCreateTimeGreaterThanEqualOrderByCreateTimeDesc(corpid, memberid,YesOrNoEnum.YES.getValue(), createTime);
			Double orderPlatTotal = 0d;
			if(orderPlats.size()>0){
					orderPlatTotal+=orderPlats.get(0).getActualPrice();
			}
			//step2 总共抽奖次数, 向下区整
			int sumCount = (int)Math.floor(orderPlatTotal/frequency);
			//step3 实际抽奖次数
			//已抽奖次数  memberid ，corpid，日期大于等于10.2
			//int count1 = lotteryRecordDao.findByLotteryIdAndMemberIdAndCorpIdAndCreateTimeGreaterThanEqual(activity.getId(),memberid,1L,createTime);
			//还剩余抽奖次数
			List<LotteryNumber> numberList= numberDao.findByMemberIdAndActivityId(memberid, activity.getId());
			int count2 = 0;
			if(numberList.size()>0){
				LotteryNumber number = numberList.get(0);
				count2 = number.getTotal();
			}
			//int actualCount = count1+count2;
			if(count2>=10){
				return new ResultObject("000", "可以抽奖", lotteryList.get(0).getId());
			}
			int count =0;
			if(sumCount+count2>=10){
				count = sumCount-(sumCount+count2-10);
			}else{
				count=sumCount;
			}
			//step4 增加用户的抽奖次数
			//int count = sumCount - actualCount;
			//如果累计大于10次，不再获得抽奖机会
		/*	if(count1==10){
				count = 0;
			}*/
			/*if(count<0){
				count=0;
			}*/
			if(numberList.size()>0){
				LotteryNumber number = numberList.get(0);
				number.setTotal(number.getTotal()+count);
				numberDao.save(number);
			}else{
				LotteryNumber number = new LotteryNumber();
				number.setTotal(count);
				number.setActivity(lotteryList.get(0));
				number.setMember(new Member(memberid));
				numberDao.save(number);
			}
			return new ResultObject("000", "可以抽奖", lotteryList.get(0).getId());
		}else{
			return new ResultObject("500", "不能抽奖", null);
		}
	}
	
	
	public static void main(String[] args) {
		int sumcount = (int)Math.floor(10.2/2);
		System.out.println(sumcount);
	}

}
